﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Brain.Editor
{
    /// <summary>
    /// Represents an Editor Scene Data
    /// </summary>
    public class EditorSceneData : IBrainSonSerialiable
    {
        /// <summary>
        /// Represents all the content loaded by the user on the editor
        /// </summary>
        public SceneContent ContentProject { get; set; }

        /// <summary>
        /// Represents all the plugins loaded by the user on the editor
        /// </summary>
        public List<LoadPlugin> LoadPlugins { get; set; }

        
        public string RootFolder { get; set; }

        public string LevelName { get; set; }

        public bool ShouldSerialize { get; set; }

        public List<IEditorPlugin> Plugins { get; set; }

        public EditorSceneData()
        {
            ContentProject = new SceneContent();
            LoadPlugins = new List<LoadPlugin>();

            Plugins = new List<IEditorPlugin>();

            ShouldSerialize = true;
        }

        public void SerializeData(SerializationData parentData, string index)
        {
            parentData.AddData("LevelData.LevelName", this.LevelName, index);

            parentData.AddData("LevelData.RootFolder", this.RootFolder, index);

            parentData.AddData("LevelData.Plugins", this.LoadPlugins.Count, index);
            for (int i = 0; i < LoadPlugins.Count; i++)
            {
                LoadPlugin p = LoadPlugins[i];

                parentData.AddData("LevelData.Plugins.AssemblyPath" + i, p.PluginAssemblyPath);
                parentData.AddData("LevelData.Plugins.Type" + i, p.PluginType);
            }
        }

        public void DeserializeData(SerializationData parentData, string index)
        {
            this.LevelName = parentData.GetData<string>("LevelData.LevelName", index);

            this.RootFolder = parentData.GetData<string>("LevelData.RootFolder", index);

            int plugins = parentData.GetData<int>("LevelData.Plugins", index);
            if (plugins > 0)
            {
                for (int i = 0; i < plugins; i++)
                {
                    string assemblyPath = parentData.GetData<string>("LevelData.Plugins.AssemblyPath" + i);
                    string pluginType = parentData.GetData<string>("LevelData.Plugins.Type" + i);
                    LoadPlugin l = new LoadPlugin();
                    l.PluginAssemblyPath = assemblyPath;
                    string path = RootFolder + assemblyPath;

#if WINDOWS
                    Assembly ass = Assembly.LoadFile(path);
#else
                    Assembly ass = Assembly.LoadFrom(path);
#endif
                    Type t = ass.GetType(pluginType);
                    l.PluginType = t;

                    this.LoadPlugins.Add(l);
                }
            }

            for (int i = 0; i < LoadPlugins.Count; i++)
            {
                LoadPlugin load = LoadPlugins[i];
                Type t = load.PluginType;
                IEditorPlugin plugin = (IEditorPlugin)Activator.CreateInstance(t);
                Plugins.Add(plugin);
                plugin.Reload();
            }
        }
        public void EndReloadPlugins()
        {
            for (int i = 0; i < Plugins.Count; i++)
            {
                Plugins[i].EndReload();
            }
        }


        public void DeserializedAll()
        {
        }
    }
}
